import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
} from '@nestjs/common';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { RequireLogin, RequirePermission } from 'src/custom.decorator';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';

@ApiTags('菜单管理')
@ApiBearerAuth()
@Controller('menu')
@RequireLogin()
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  @Post()
  @RequirePermission('menu:create')
  @ApiOperation({ summary: '创建菜单' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiResponse({ status: 400, description: '菜单已存在' })
  async create(@Body() createMenuDto: CreateMenuDto) {
    return await this.menuService.create(createMenuDto);
  }

  @Get('tree')
  @RequirePermission('menu:query')
  @ApiOperation({ summary: '获取菜单树' })
  async findTree() {
    return await this.menuService.findTree();
  }

  @Get(':id')
  @RequirePermission('menu:query')
  @ApiOperation({ summary: '获取菜单详情' })
  @ApiParam({ name: 'id', description: '菜单ID' })
  async findOne(@Param('id') id: string) {
    return await this.menuService.findOne(+id);
  }

  @Put(':id')
  @RequirePermission('menu:update')
  @ApiOperation({ summary: '更新菜单' })
  @ApiParam({ name: 'id', description: '菜单ID' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiResponse({ status: 400, description: '菜单不存在/菜单名已存在' })
  async update(@Param('id') id: string, @Body() updateMenuDto: UpdateMenuDto) {
    return await this.menuService.update(+id, updateMenuDto);
  }

  @Delete(':id')
  @RequirePermission('menu:delete')
  @ApiOperation({ summary: '删除菜单' })
  @ApiParam({ name: 'id', description: '菜单ID' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 400, description: '菜单不存在/请先删除子菜单' })
  async remove(@Param('id') id: string) {
    return await this.menuService.remove(+id);
  }
}
